package com.liuli.action;



import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import com.liuli.entity.Resume;
import com.liuli.entity.UserInfo;
import com.liuli.service.UserService;
import com.liuli.utils.DateUtil;
import com.liuli.utils.EmailManager;
import com.liuli.utils.ReturnJson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
public class UserInfoAction  extends BaseAction implements ModelDriven<UserInfo>{
	
	
    private UserService us ;

    private UserInfo userInfo;
    
    private List<UserInfo> list;
    
  
    /**
     * 更新用户基本信息
     */
    public void  editUser(){
		JSONObject json = new JSONObject();
		try {
			UserInfo user = us.findById(userInfo.getUid());
			if(userInfo.getNickname()!=null){
				user.setNickname(userInfo.getNickname());
			}else if(userInfo.getIntro()!=null){
				user.setIntro(userInfo.getIntro());
			}
			us.update(user);
			json.put("errcode", 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	  ReturnJson.reponse(json.toString());
	}
   
    /**
    * 登录
    */
    public void login(){
    	JSONObject json = new JSONObject();
       UserInfo user = us.login(userInfo.getUsername(), userInfo.getPassword());
       if(user !=null){
    	     if(user.getStatu().equals("1")){  //激活状态 
    	    	//用户名写入 session 
	    		getSession().put("username", user.getUsername());
	    		getSession().put("balance", user.getBalance()); //表示个人还是企业 0是个人，1表示企业
	    		getSession().put("uid", user.getUid());
	    		json.put("username",user.getUsername());
	    		json.put("password",user.getPassword());
	    		json.put("uid",user.getUid());
	    		//用户登录成功
	    	   json.put("errcode", "0");
    	     }else{
    	    	 json.put("errcode", "40012");  //用户未激活
    	     }
    	}else{
    		//用户密码错误，登录失败
    		json.put("errcode", "40010");
    	}
    	ReturnJson.reponse(json.toString());
    }
    /**
     * 退出 
     */
	 public void logout(){
		 JSONObject json = new JSONObject();
		 getSession().remove("username");
		 getSession().remove("balance");
		 json.put("errcode", "0");
		 ReturnJson.reponse(json.toString());
	 }
	 /**
	  * 注册
	  */
	 public void register(){
		JSONObject json = new JSONObject();
		DateUtil dateutils = new DateUtil();
		String code =dateutils.getTimeRand();
		try {
			//判断用户是否存在
			UserInfo user =us.isUserExits(userInfo.getUsername());
			if(user!=null){
				json.put("errcode", "40012");
			}else{
				json.put("errcode", "0");
				userInfo.setCode(code);
				us.register(userInfo);
				getSession().put("code",code);
			}
		} catch (Exception e) {
			json.put("errcode", "40001");
			e.printStackTrace();
		}
		 ReturnJson.reponse(json.toString());
		 
	 }
	 
	/**
	 * 邮箱激活
	 * @return
	 */
	public String active(){
		
		String oldcode = (String) getSession().get("code");
		if(oldcode!=null){
			if(oldcode.equals(userInfo.getCode())){  //激活码相同就去修改用户状态
			UserInfo user =us.isUserExits(userInfo.getUsername());
			user.setStatu("1");  //设置用户状态，表示激活
				us.update(user);
			}else{  //表示过期  重新发送邮件
				return "email"; 
			}
		}
		return "input";
	}
    @Autowired
    @Qualifier("us")
	public UserService getUs() {
		return us;
	}

	public void setUs(UserService us) {
		this.us = us;
	}



	public UserInfo getUserInfo() {
		return userInfo;
	}
	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}
	@Override
	public UserInfo getModel() {
		if(userInfo==null){
			userInfo = new UserInfo();
		}
		return userInfo;
	}

	public List<UserInfo> getList() {
		return list;
	}

	public void setList(List<UserInfo> list) {
		this.list = list;
	}
	 
	
	   
	   
	   
	

}
